﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucContactAddUpdate : UserControl
    {
        private readonly ContactService _contactService = new ContactService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int insert = Int32.Parse(Request.QueryString["in"]);
                if (insert == 1)
                {
                    RefreshControl();
                }
                else
                {
                    int id = Int32.Parse(Request.QueryString["id"]);
                    DataTable dt = _contactService.Contact_GetById(id);
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    txtCompany.Text = dt.Rows[0]["company"].ToString();
                    txtAddress.Text = dt.Rows[0]["address"].ToString();
                    txtDate.Text = dt.Rows[0]["date"].ToString();
                    txtEmail.Text = dt.Rows[0]["mail"].ToString();
                    txtTel.Text = dt.Rows[0]["tel"].ToString();
                }
            }
        }

        private void RefreshControl()
        {
            txtCompany.Text = string.Empty;
            txtDate.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtName.Text = string.Empty;
            txtTel.Text = string.Empty;
            txtAddress.Text = string.Empty;
        }

        protected void btnCancelAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucContactView");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var contact = new ContactInfo
                              {
                                  Name = txtName.Text,
                                  Company = txtCompany.Text,
                                  Address = txtAddress.Text,
                                  Detail = CKEditor1.Text,
                                  Tel = txtTel.Text,
                                  Date = DateTime.Parse(txtDate.Text),
                                  Mail = txtEmail.Text
                              };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _contactService.Contact_Insert(contact);
            }
            else
            {
                contact.Id = Int32.Parse(Request.QueryString["id"]);
                _contactService.Contact_Update(contact);
            }
            Response.Redirect("~/Admin/View.aspx?action=ucAdvertiseView");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            var contact = new ContactInfo
                              {
                                  Name = txtName.Text,
                                  Company = txtCompany.Text,
                                  Address = txtAddress.Text,
                                  Detail = CKEditor1.Text,
                                  Tel = txtTel.Text,
                                  Date = DateTime.Parse(txtDate.Text),
                                  Mail = txtEmail.Text
                              };
            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _contactService.Contact_Insert(contact);
            }
            else
            {
                contact.Id = Int32.Parse(Request.QueryString["id"]);
                _contactService.Contact_Update(contact);
            }
        }
    }
}